Ext.define('SKJF.controller.MSTACCOUNTSKJF',{
	extend: 'Ext.app.Controller',
	views: ['MASTER.v_mstaccountskjf','MASTER.v_mstaccountskjf_form'],
	models: ['m_mstaccountskjf'],
	stores: ['s_mstaccountskjf'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listmstaccountskjf',
		selector: 'Listmstaccountskjf'
	}, {
		ref: 'v_mstaccountskjf_form',
		selector: 'v_mstaccountskjf_form'
	}, {
		ref: 'Savemstaccountskjf',
		selector: 'v_mstaccountskjf_form #save'
	}, {
		ref: 'Createmstaccountskjf',
		selector: 'v_mstaccountskjf_form #create'
	}, {
		ref: 'MSTACCOUNTSKJF',
		selector: 'MSTACCOUNTSKJF'
	}],


	init: function(){
		this.control({
			'MSTACCOUNTSKJF': {
				'afterrender': this.mstaccountskjfAfterRender
			},
			'v_mstaccountskjf_form': {
				'afterlayout': this.mstaccountskjfAfterLayout
			},
			'Listmstaccountskjf': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListmstaccountskjf
			},
			'Listmstaccountskjf button[action=create]': {
				click: this.createRecord
			},
			'Listmstaccountskjf button[action=delete]': {
				click: this.deleteRecord
			},
			'Listmstaccountskjf button[action=print]': {
				click: this.printRecords
			},
			'v_mstaccountskjf_form button[action=save]': {
				click: this.saveV_mstaccountskjf_form
			},
			'v_mstaccountskjf_form button[action=create]': {
				click: this.saveV_mstaccountskjf_form
			},
			'v_mstaccountskjf_form button[action=cancel]': {
				click: this.cancelV_mstaccountskjf_form
			}
		});
	},
	
	mstaccountskjfAfterRender: function(){
		var mstaccountskjfStore = this.getListmstaccountskjf().getStore();
		mstaccountskjfStore.load();
	},
	
	mstaccountskjfAfterLayout: function(){this.getV_mstaccountskjf_form().down('#BANK_ACCOUNT_field').focus(false, true);this.getV_mstaccountskjf_form().down('#ACCOUNT_NO_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListmstaccountskjf	= this.getListmstaccountskjf();
		var getV_mstaccountskjf_form= this.getV_mstaccountskjf_form(),
			form			= getV_mstaccountskjf_form.getForm();
		var getSavemstaccountskjf	= this.getSavemstaccountskjf();
		var getCreatemstaccountskjf	= this.getCreatemstaccountskjf();
		
		/* grid-panel */
		getListmstaccountskjf.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_mstaccountskjf_form.down('#BANK_ACCOUNT_field').setReadOnly(false);getV_mstaccountskjf_form.down('#ACCOUNT_NO_field').setReadOnly(false);
		getSavemstaccountskjf.setDisabled(true);
		getCreatemstaccountskjf.setDisabled(false);
		getV_mstaccountskjf_form.setDisabled(false);
		
		this.getMSTACCOUNTSKJF().setActiveTab(getV_mstaccountskjf_form);		
	},
	
	enableDelete: function(dataview, selections){
		this.getListmstaccountskjf().down('#btndelete').setDisabled(!selections.length);
	},
	
	updateListmstaccountskjf: function(me, record, item, index, e){
		var getMSTACCOUNTSKJF		= this.getMSTACCOUNTSKJF();
		var getListmstaccountskjf	= this.getListmstaccountskjf();
		var getV_mstaccountskjf_form= this.getV_mstaccountskjf_form(),
			form			= getV_mstaccountskjf_form.getForm();
		var getSavemstaccountskjf	= this.getSavemstaccountskjf();
		var getCreatemstaccountskjf	= this.getCreatemstaccountskjf();
		
		getSavemstaccountskjf.setDisabled(false);
		getCreatemstaccountskjf.setDisabled(true);
		getV_mstaccountskjf_form.down('#BANK_ACCOUNT_field').setReadOnly(true);getV_mstaccountskjf_form.down('#ACCOUNT_NO_field').setReadOnly(true);		
		getV_mstaccountskjf_form.loadRecord(record);
		
		getListmstaccountskjf.setDisabled(true);
		getV_mstaccountskjf_form.setDisabled(false);
		getMSTACCOUNTSKJF.setActiveTab(getV_mstaccountskjf_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListmstaccountskjf().getStore();
		var selection = this.getListmstaccountskjf().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "BANK_ACCOUNT" = "'+selection.data.BANK_ACCOUNT+'","ACCOUNT_NO" = "'+selection.data.ACCOUNT_NO+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListmstaccountskjf().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_mstaccountskjf/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/mstaccountskjf.html','mstaccountskjf_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_mstaccountskjf_form: function(){
		var getMSTACCOUNTSKJF		= this.getMSTACCOUNTSKJF();
		var getListmstaccountskjf 	= this.getListmstaccountskjf();
		var getV_mstaccountskjf_form= this.getV_mstaccountskjf_form(),
			form			= getV_mstaccountskjf_form.getForm(),
			values			= getV_mstaccountskjf_form.getValues();
		var store 			= this.getStore('s_mstaccountskjf');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_mstaccountskjf/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('BANK_ACCOUNT') === values.BANK_ACCOUNT && record.get('ACCOUNT_NO') === values.ACCOUNT_NO) {
										return true;
									}
									return false;
								}
							);
							/* getListmstaccountskjf.getView().select(recordIndex); */
							getListmstaccountskjf.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_mstaccountskjf_form.setDisabled(true);
					getListmstaccountskjf.setDisabled(false);
					getMSTACCOUNTSKJF.setActiveTab(getListmstaccountskjf);
				}
			});
		}
	},
	
	createV_mstaccountskjf_form: function(){
		var getMSTACCOUNTSKJF		= this.getMSTACCOUNTSKJF();
		var getListmstaccountskjf 	= this.getListmstaccountskjf();
		var getV_mstaccountskjf_form= this.getV_mstaccountskjf_form(),
			form			= getV_mstaccountskjf_form.getForm(),
			values			= getV_mstaccountskjf_form.getValues();
		var store 			= this.getStore('s_mstaccountskjf');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_mstaccountskjf/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_mstaccountskjf_form.setDisabled(true);
					getListmstaccountskjf.setDisabled(false);
					getMSTACCOUNTSKJF.setActiveTab(getListmstaccountskjf);
				}
			});
		}
	},
	
	cancelV_mstaccountskjf_form: function(){
		var getMSTACCOUNTSKJF		= this.getMSTACCOUNTSKJF();
		var getListmstaccountskjf	= this.getListmstaccountskjf();
		var getV_mstaccountskjf_form= this.getV_mstaccountskjf_form(),
			form			= getV_mstaccountskjf_form.getForm();
			
		form.reset();
		getV_mstaccountskjf_form.setDisabled(true);
		getListmstaccountskjf.setDisabled(false);
		getMSTACCOUNTSKJF.setActiveTab(getListmstaccountskjf);
	}
	
});